<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php 
	$USER = getAccountUserObject(true);
	
	unset($_SESSION['SERVICES_INFO']); 
	//$myPlaceListArray = explode(',', $ESCORT->getPlacesListIdsContract());
	
	//breadcumb(4, array($ESCORT->getEscortName(),'#'));
	
	$subscriptionArray = array(); 
	$subsJsonArray = array();
	foreach(getSubcriptionData_ioc() as $key=>$arrayValue){
		$subscriptionArray[$key] =  $arrayValue[0].' '.$arrayValue[1].' &euro;';
		$subsJsonArray[$key] = $arrayValue[1];
	}
	 
	$listPlaceArray = array();
	$placeJsonArray = array();
	foreach(getPlaceListWorkData_ioc() as $key=>$arrayValue){
		$listPlaceArray[$key] = $arrayValue[0].' '.$arrayValue[1].' &euro;';
		$placeJsonArray[$key] = $arrayValue[1];
	}
	
	$listPackArray = array();
	$packJsonArray = array();
	foreach(getPackListData_ioc() as $key=>$arrayValue){
		$listPackArray[$key] = $arrayValue[0].' '.$arrayValue[1].' &euro;';
		$packJsonArray[$key] = $arrayValue[1];
	}
	
	$listPriorityArray = array();
	$priorityJsonArray = array();
	foreach(getPriorityData_ioc() as $key=>$arrayValue){
		$listPriorityArray[$key] = $arrayValue[0].' '.$arrayValue[1].' &euro;' .' (+'.$arrayValue[2].')';
		$priorityJsonArray[$key] = $arrayValue[1];
	}
	$totalPeriodDiscount = $totalVolumnDiscount = 0; 
	$iC = 0;
?>


<script type="text/javascript">
	;var DISCOUNT = JSON.parse('<?php echo json_encode(contractDiscount_ioc());?>');
	var subs = JSON.parse('<?php echo json_encode($subsJsonArray);?>');
	var place = JSON.parse('<?php echo json_encode($placeJsonArray);?>');
	var pack = JSON.parse('<?php echo json_encode($packJsonArray);?>');
	var priority = JSON.parse('<?php echo json_encode($priorityJsonArray);?>');
	var EURO_MONTHLY = ' &euro;';
	var VOLUMN_DISC_AMOUNT = 0;
	var VOLUMN_DISC_UNIT = '';
	
	function returnValueJSON(json,key){
		return parseFloat( json[key] );
	}
</script>

<form action="<?php echo site_url('agent/transaction_submit/submit_services');?>" name="submit_services" id="submit_services" method="post" enctype="multipart/form-data" >

<div id="content-left">
	<div id="personal-tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
		<div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
			<!-- content -->	
			 
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-right">
					<?php echo language_translate('services_page_your_current_service');?>
				</div>
				<div class="clear"></div>
			</div>
			<?php 
				$PAYMENT = $this->payment_log_entity->getCurrentService_Agent($USER->getId());
				$subscription_id = $this->payment_log_entity->getServicesTypePaid_Agent($USER->getId(), 'SUBS');
				$myPlaceListArray = array_filter( explode(',', $this->payment_log_entity->getServicesTypePaid_Agent($USER->getId(), 'PLACE')) );
				$myPackListArray = array_filter( explode(',', $this->payment_log_entity->getServicesTypePaid_Agent($USER->getId(), 'PACK')) );
				$myPriorityListArray = array_filter( explode(',', $this->payment_log_entity->getServicesTypePaid_Agent($USER->getId(), 'PRI')) );
			?>
			
			<div class="dashboard-info-wrapper">
				<div class="select-box">
					<table width="100%">
						<thead>
							<th><?php echo language_translate('services_page_table_head_servicename');?></th>
							<th><?php echo language_translate('services_page_table_head_details');?></th>
							<th><?php echo language_translate('contract_start_date');?></th>
							<th><?php echo language_translate('contract_to_date');?></th>
							<th><?php echo language_translate('services_page_table_head_period');?></th>
							<th style="text-align:right;"><?php echo language_translate('services_page_table_head_priceeuro');?></th>
						</thead>
						<tbody>
							<?php if($subscription_id):?>
								<tr>
									<?php 
										$SUB = $this->payment_log_entity->checkServiceExisted_Agent($USER->getId(), 'SUBS', $subscription_id);
										$sub_period = $SUB ? $SUB->getLatestPeriod() : 1;
										$subs_array = getSubcriptionData_ioc($subscription_id);
										$totalPeriodDiscount += $SUB->discount_period_price;
										$totalVolumnDiscount += $SUB->discount_volumn_price;
										$iC++;
									?>
									<td><?php echo language_translate('services_page_subscription');?></td>
									<td><?php echo $subs_array[0];?></td>
									<td><?php echo sysDateFormat( $SUB->latest_payment_date ,DATE_FORMAT );?></td>
									<td><?php echo sysDateFormat( $SUB->apply_date ,DATE_FORMAT );?></td>
									<td><?php echo $sub_period;?></td>
									<td style="text-align:right;"><?php echo euroCurrencyFormat( number_format( $SUB->getPaymentPrice(), 2 ) );?></td>
								</tr>
							<?php endif;?>
							<?php
								foreach($myPlaceListArray as $k):
									$placeInfo = getPlaceListDataInfoById($k);
							?>
								<?php if($placeInfo):?>
									<?php 
										$id=$placeInfo['id'];
										$PLACE = $this->payment_log_entity->checkServiceExisted_Agent($USER->getId(), 'PLACE', $id);
										$place_period = $PLACE ? $PLACE->getLatestPeriod() : 1;
										$totalPeriodDiscount += $PLACE->discount_period_price;
										$totalVolumnDiscount += $PLACE->discount_volumn_price;
										$iC++;
									?>
									<tr>
										<td><?php echo language_translate('services_page_where_do_you_work');?></td>
										<td>
											<?php echo $placeInfo['country'];?>
											<?php if($placeInfo['state']):?> 
												/
												<?php echo $placeInfo['state'];?>
											<?php endif;?>	
											<?php if($placeInfo['province']):?> 
												/
												<?php echo $placeInfo['province'];?>
											<?php endif;?>
											<?php if($placeInfo['city']):?> 
												/
												<?php echo $placeInfo['city'];?>
											<?php endif;?>
										</td>
										<td><?php echo sysDateFormat( $PLACE->latest_payment_date ,DATE_FORMAT );?></td>
										<td><?php echo sysDateFormat( $PLACE->apply_date ,DATE_FORMAT );?></td>
										<td>
											<?php echo $place_period;?>
										</td>
										<td style="text-align:right;"><?php echo euroCurrencyFormat( number_format( $PLACE->getPaymentPrice(), 2 ) );?></td>
									</tr>
								<?php endif;?>
							<?php endforeach; ?>
							
							<?php foreach($myPackListArray as $k):?>
									<?php 
										$id=$k;
										$PACK = $this->payment_log_entity->checkServiceExisted_Agent($USER->getId(), 'PACK', $id);
										$pack_period = $PACK ? $PACK->getLatestPeriod() : 1;
										$pack_array = getPackListData_ioc($id);
										$totalPeriodDiscount += $PACK->discount_period_price;
										$totalVolumnDiscount += $PACK->discount_volumn_price;
										$iC++;
									?>
									<tr>
										<td><?php echo language_translate('services_page_add_a_pack');?></td>
										<td>
											<?php echo $pack_array[0];?>
										</td>
										<td><?php echo sysDateFormat( $PACK->latest_payment_date ,DATE_FORMAT );?></td>
										<td><?php echo sysDateFormat( $PACK->apply_date ,DATE_FORMAT );?></td>
										<td>
											<?php echo $pack_period;?>
										</td>
										<td style="text-align:right;"><?php echo euroCurrencyFormat( number_format( $PACK->getPaymentPrice(), 2 ) );?></td>
									</tr>
							<?php endforeach; ?>
							
							<?php foreach($myPriorityListArray as $k):?>
									<?php 
										$id=$k;
										$PRI = $this->payment_log_entity->checkServiceExisted_Agent($USER->getId(), 'PRI', $id);
										$pri_period = $PRI ? $PRI->getLatestPeriod() : 1;
										$pri_array = getPriorityData_ioc($id);
										$totalPeriodDiscount += $PRI->discount_period_price;
										$totalVolumnDiscount += $PRI->discount_volumn_price;
										$iC++;
									?>
									<tr>
										<td><?php echo language_translate('services_page_priority');?></td>
										<td>
											<?php echo $pri_array[0];?>
										</td>
										<td><?php echo sysDateFormat( $PRI->latest_payment_date ,DATE_FORMAT );?></td>
										<td><?php echo sysDateFormat( $PRI->apply_date ,DATE_FORMAT );?></td>
										<td>
											<?php echo $pri_period;?>
										</td>
										<td style="text-align:right;"><?php echo euroCurrencyFormat( number_format( $PRI->getPaymentPrice(), 2 ) );?></td>
									</tr>
							<?php endforeach; ?>
							<tr>
								<td colspan=4><?php echo language_translate('services_page_escorts_apply_number_1');?></td>
								<td><?php if($iC) echo $USER->escorts_apply_number;?></td>
								<td></td>
							</tr>
							<tr>
								<td colspan=5><?php echo language_translate('services_page_net_amount');?></td>
								<td style="text-align:right;"><?php if($PAYMENT AND $iC) echo euroCurrencyFormat($PAYMENT);?></td>
							</tr>
						</tbody>
					</table>
				 </div>
				<div class="clear"></div>
			</div>
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left"><span><?php echo language_translate('contract_volumn_discount');?></span></div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<?php echo euroCurrencyFormat(number_format($totalVolumnDiscount,2));?> &euro;
					</div>
					<div class="clear"></div>
				</div>
				<div class="clear"></div>
			</div>
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left"><span><?php echo language_translate('services_page_total_period_discount');?></span></div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<?php echo euroCurrencyFormat(number_format($totalPeriodDiscount,2));?> &euro;
					</div>
					<div class="clear"></div>
				</div>
				<div class="clear"></div>
				<hr class="background-separate"/>
			</div>
		
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-right">
					<?php echo language_translate('services_page_edit_your_service');?>
				</div>
				<div class="clear"></div>
			</div>
			
		<?php 
			$subscription_id = $this->payment_log_entity->getServiceTypeStringIdsCurrently($USER->getId(), 'SUBS');
			$myPlaceListArray = explode(',', $this->payment_log_entity->getServiceTypeStringIdsCurrently($USER->getId(), 'PLACE') );
			$myPackListArray = explode(',', $this->payment_log_entity->getServiceTypeStringIdsCurrently($USER->getId(), 'PACK') );
			$myPriorityListArray = explode(',', $this->payment_log_entity->getServiceTypeStringIdsCurrently($USER->getId(), 'PRI') );
			$period = $this->payment_log_entity->getServicePeriodCurrently($USER->getId());
		?>
			
			<div class="clear"></div>
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_subscription');?></span>
					<div class="help-text" title="<?php echo language_translate('services_page_subscription_hint');?>"></div>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<?php echo form_dropdown('subscription', $subscriptionArray, array($subscription_id) , 'id="subscription" class="normal"' );?>
					</div>
					<div class="select-box">
						<?php 
							//$SUB = $this->payment_log_entity->checkServiceExisted_Agent($USER->getId(), 'SUBS', $subscription_id);
							//$sub_period = $SUB ? $SUB->getLatestPeriod() : 1;
						?>
						<?php echo contractDiscountSelectionForm('subscription_period','subs','onchange="return cal_discount_subs(this.value);" name="subscription_period"', $period );?>
					</div>
				 </div>
				<div class="clear"></div>
			</div>	
	
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_where_do_you_work');?></span>
					<div class="help-text" title="<?php echo language_translate('services_page_where_do_you_work_hint');?>"></div>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box" style="margin:0px;">
						<?php 
							//echo form_dropdown('place_listing', $listPlaceArray, array() , 'id="place_listing" class="normal" onchange="return loadListingPlaceElement(this.value);"' );
							$this->load->model('home/escort_zone_m');
							//echo $this->home_m->listingPlaceLocation();
						?>
						<div id="service_country">
							<?php echo $this->escort_zone_m->services_load_country();?>
						</div>
						
					<!--	<div class="clear" style="height:5px;"></div>	-->
						<div id="service_state">
							<?php echo $this->escort_zone_m->services_load_state(0);?>
						</div>
						
					<!--	<div class="clear" style="height:5px;"></div>	-->
						<div id="service_province">
							<?php echo $this->escort_zone_m->services_load_province(0,0);?>
						</div>
					<!--	
						<div class="clear" style="height:5px;"></div>
						<div id="service_city">
							<?php //echo $this->escort_zone_m->services_load_city(0,0,0);?>
						</div>
					-->	
						<div class="clear" style="height:5px;"></div>
							<?php echo $this->escort_zone_m->add_button_js();?>
						<div class="clear"></div>	
						<?php echo loader_image_s("id='listingPlaceLoadingImage' class='hidden'");?>
					</div>
					<div class="clear"></div>
					<div class="select-box" style="margin-top:5px; ">
						<table width="100%">
							<thead>
								<th><?php echo language_translate('services_page_table_head_country');?> 
								 / <?php echo language_translate('services_page_table_head_state');?> 
								 / <?php echo language_translate('services_page_table_head_province');?> 
								 / <?php echo language_translate('services_page_table_head_city');?></th>
								<th><?php echo language_translate('services_page_table_head_price');?></th>
								<th><?php echo language_translate('services_page_table_head_action');?></th>
								 
							</thead>
							<tbody id="listingPlaceTarget">
								<?php
									foreach($myPlaceListArray as $k):
									$placeInfo = getPlaceListDataInfoById($k);
								?>
									<?php if($placeInfo):?>
									<?php $id=$placeInfo['id'];?>
									<tr id="listing_id_<?php echo $id;?>">
										<td>
											<?php echo $placeInfo['country'];?>
											<?php if($placeInfo['state']):?> 
												/
												<?php echo $placeInfo['state'];?>
											<?php endif;?>	
											<?php if($placeInfo['province']):?> 
												/
												<?php echo $placeInfo['province'];?>
											<?php endif;?>
											<?php if($placeInfo['city']):?> 
												/
												<?php echo $placeInfo['city'];?>
											<?php endif;?>
										</td>
										<td>
											<?php echo $placeInfo['price'].' &euro;';?>
										</td>
										<td>
											<?php echo delete_image("class='deleteItem' onclick='return deleteListingItem($id);'");?>
										</td>
									
										<input type="hidden" name="list_place[]" value="<?php echo $id;?>" />
									</tr>
									<?php endif;?>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				 <div class="clear"></div>
				 <div id="placeLISTwarning"><?php echo language_translate('contract_listing_place_warning');?></div>	
				 </div>
				
				<div class="clear"></div>
				
			</div>
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_add_a_pack');?></span>
					<div class="help-text" title="<?php echo language_translate('services_page_add_a_pack_hint');?>"></div>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<table width="100%">
							<thead>
								<th><?php echo language_translate('services_page_table_head_packname');?> 
								<th><?php echo language_translate('services_page_table_head_price');?></th>
								<th><?php echo language_translate('services_page_table_head_action');?></th>
							</thead>
							<tbody>
								<?php
									foreach(getPackListData_ioc() as $k =>$v):
								?>
									<tr>
										<td>
											<?php echo $v[0];?>
										</td>
										<td>
											<?php echo $v[1].' &euro;';?>
										</td>
										<td>
											<?php 
												if(in_array($k, $myPackListArray)){
													$checked = 'checked="checked"';
												}else{
													$checked = '';
												}
											?>
											<input type="checkbox" name="list_pack[]" value="<?php echo $k;?>" <?php echo $checked;?> class="custom" />
										</td>
										
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				 </div>
				<div class="clear"></div>
			</div>
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_priority');?></span>
					<div class="help-text" title="<?php echo language_translate('services_page_priority_hint');?>"></div>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<table width="100%">
							<thead>
								<th><?php echo language_translate('services_page_table_head_priority');?> 
								<th><?php echo language_translate('services_page_table_head_price');?></th>
								<th><?php echo language_translate('services_page_table_head_action');?></th>
							</thead>
							<tbody>
								<?php
									foreach(getPriorityData_ioc() as $k =>$v):
								?>
									<tr>
										<td>
											<?php echo $v[0].' (+'.$v[2].')';?>
										</td>
										<td>
											<?php echo $v[1].' &euro;';?>
										</td>
										<td>
											<?php 
												if(in_array($k, $myPriorityListArray)){
													$checked = 'checked="checked"';
												}else{
													$checked = '';
												}
											?>
											<!--
											<input type="checkbox" name="list_priority[]" value="<?php echo $k;?>" <?php echo $checked;?> class="custom" />
											-->
											<input type="radio" name="list_priority" value="<?php echo $k;?>" <?php echo $checked;?> class="custom" />
											
										</td>
										
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>	
						<?php
							//echo multi_checkbox('list_priority[]',$listPriorityArray,explode(',', $ESCORT->getPriorityIds()));				
						?>
					</div>
				 </div>
				<div class="clear"></div>
			</div>
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_escorts_apply_number');?></span>
					<div class="help-text" title="<?php echo language_translate('services_page_escorts_apply_number_hint');?>"></div>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<?php echo form_dropdown('escorts_apply_number', escortApplyNumberArray(), array($USER->getEscortsApplyNumber()) , 'id="escorts_apply_number" class="normal" ' );?>
					</div>
					<span style="float:left;"><?php echo loader_image_s('id="id_loader_pic_1" class="hidden"');?></span>
				</div>
				<div class="clear"></div>
			</div>	
			
			<div class="clear"></div>
			
			<div class="dashboard-info-left">
				<span><?php echo language_translate('contract_volumn_discount_info');?></span>
			</div>
			<div class="dashboard-info-right">
				<div class="select-box">
					<span id="volumn_discount_info" style="float:left;"></span> 
				</div>
			</div>
			
			<div class="clear"></div>
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_total_price');?></span>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<span id="total_price" style="float:left;"></span> 
					</div>
				</div>
				
				<div class="clear" style="margin:3px 0px;"></div>
				
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_total_period_discount');?></span>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<span id="period_discount_price" style="float:left;"></span> 
					</div>
				</div>
				
				<div class="clear" style="margin:3px 0px;"></div>
				
				<div class="dashboard-info-left">
					<span><?php echo language_translate('contract_volumn_discount');?></span>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<span id="volumn_discount_price" style="float:left;"></span> 
					</div>
				</div>
				
				<div class="clear"></div>
				
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_current');?></span>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<span id="current_price" style="float:left;"></span> 
					</div>
				</div>
				
				<div class="clear" style="margin:3px 0px;"></div>
				
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_net_amount');?></span>
				</div>
				<div class="dashboard-info-right">
					<span style="float:left;" id="net_amount_price"></span>
				</div>
				
				<div class="clear"></div>
			</div>
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left">
					<span><?php echo language_translate('services_page_payment_gateway');?></span>
					<div class="help-text" title="<?php echo language_translate('services_page_payment_gateway_hint');?>"></div>
				</div>
				<div class="dashboard-info-right">
					<div class="select-box">
						<?php echo form_dropdown('payment_gateway', paymentGatewayType(), array() , 'id="payment_gateway" class="normal"' );?>
					</div>
				 </div>
				<div class="clear"></div>
			</div>	
			
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left"><span>&nbsp;</span></div>
				<div class="dashboard-info-right">
					<input type="submit" class="save-button" value="<?php echo language_translate('services_page_contract');?>" /> 
					<?php echo loader_image("id='loader_image' class='hidden'");?>
					<input type="button" onclick="return gotoPaymentPage();" id="payment-button" class="save-button hidden" 
						value="<?php echo language_translate('services_page_pay');?>" /> 
				</div>
				<div class="clear"></div>
			</div>	 
			
			<div class="dashboard-info-wrapper">
				<div id="update-message"><?php echo language_translate('update_successfully_message');?></div>
				<div class="clear"></div>
			</div>
			 
			<!-- end content -->
		</div>
	</div>
</div>

</form>

<?php $this->load->view('agent/right_nav'); ?>

<script type="text/javascript">
	var CONTRACT_ENABLE = 0;
	$(document).ready(function() { 
		$('.help-text[title]').qtip({ style: { name: 'dark', tip: true } });
	});
		
	function getVolumnDiscount(){
		$('#id_loader_pic_1').toggle();
		$.post(BASE_URI+'agent/transaction_submit/getVolumnDiscount',{number:$('#escorts_apply_number').val()},function(res){
			 VOLUMN_DISC_AMOUNT = parseInt( res.amount );
			 VOLUMN_DISC_UNIT = res.unit;
			 $('#id_loader_pic_1').toggle();
			 $('#volumn_discount_info').html('');
			 if(VOLUMN_DISC_AMOUNT > 0){
				if(VOLUMN_DISC_UNIT == 'PERCENT'){
					$('#volumn_discount_info').text(VOLUMN_DISC_AMOUNT+'%');
				}else{
					$('#volumn_discount_info').text(VOLUMN_DISC_AMOUNT+'&euro;');
				}
			 }
			 calNetAmount();
		},'json');
	}
	
	function calNetAmount(){
		$('#id_loader_pic_1').toggle();
		
		var number_escort = $('#escorts_apply_number').val();
		var period = $('#subscription_period').val();
		var subscription_id = $('#subscription').val();
		var list_place = getArrayNameTextInput('list_place').join(',');
		var list_pack = getMultiCheckbox('list_pack').join(',');
		var list_priority = $('input[name="list_priority"]:checked').val();//getMultiCheckbox('list_priority').join(',');
		list_priority = (list_priority) ? list_priority:'';
		
		CONTRACT_ENABLE = 0;
		$.post(
			BASE_URI+'agent/transaction_submit/calNetAmount',
			{	
				number_escort:number_escort,period:period,
				subscription_id:subscription_id,list_place:list_place,
				list_pack:list_pack,list_priority:list_priority
			},
			function(res){
				$('#net_amount_price').html(res.netamount + EURO_MONTHLY);
				$('#volumn_discount_price').html(res.volumndiscount+ EURO_MONTHLY);
				$('#total_price').html(res.totalprice+ EURO_MONTHLY);
				$('#period_discount_price').html(res.perioddiscount+ EURO_MONTHLY);
				$('#current_price').html(res.currentprice+ EURO_MONTHLY);
				
				$('#id_loader_pic_1').toggle();
				CONTRACT_ENABLE = 1;
			}
		,'json');
	}
	
	$(document).ready(function() {
		var options = { 
			beforeSubmit:  validateB4Submit,  
			success:       processAfterResponding   
		};	
		$('#submit_services').ajaxForm(options); 
		
		$('#subscription,#escorts_apply_number').live('change',function(){	 
			updatePriceJS();
		});
		$('.jquery-safari-checkbox').bind('click',function(){updatePriceJS();});
		$('input[name="list_priority"]').bind('click',function(){updatePriceJS();});
		updatePriceJS();
	});	
	
	function updatePriceJS(){
		/* var net_price = parseFloat( calPriceTotalUnDiscount() - calPeriodTotalDiscount() ); 
		net_price = net_price.toFixed(2);
		net_price = (net_price <= 0) ? 0:net_price;
		
		$('#current_price').html(net_price + EURO_MONTHLY);
		$('#total_price').html( calPriceTotalUnDiscount() + EURO_MONTHLY );
		$('#period_discount_price').html( calPeriodTotalDiscount() + EURO_MONTHLY );
		*/
		getVolumnDiscount();	
	}
	
	function calPriceTotalPeriodDiscount(){
		var price = 0;
		price += cal_Subs();
		price += cal_Listing();
		price += cal_Pack();
		price += cal_Priority();
		price = price.toFixed(2);
		return price;
	}
	
	function calPeriodTotalDiscount(){
		$sub_period = $('#subscription_period').val();
		$subscription_id = $('#subscription').val();
		var price = returnValueJSON(subs,$subscription_id);
		var discount = returnValueJSON(DISCOUNT,$sub_period);
		var endPrice = (price*discount)/100;
		
		var totalPrice = calPriceTotalUnDiscount();
		endPrice = totalPrice*discount/100; 
		return endPrice.toFixed(2);
		/*
		$list_place = getArrayNameTextInput('list_place');
		for(var i=0; i<$list_place.length; i++){
			var orgPrice = returnValueJSON(place,$list_place[i]);
			discount = returnValueJSON(DISCOUNT,$sub_period);
		    endPrice += (orgPrice*discount)/100;
		}
		
		$list_pack = getMultiCheckbox('list_pack');
		for(var i=0; i<$list_pack.length; i++){
			var orgPrice = returnValueJSON(pack,$list_pack[i]);
			discount = returnValueJSON(DISCOUNT,$sub_period);
		    endPrice += (orgPrice*discount)/100;
		}
		
		$list_priority = getMultiCheckbox('list_priority');
		for(var i=0; i<$list_priority.length; i++){
			var orgPrice = returnValueJSON(priority,$list_priority[i]);
			discount = returnValueJSON(DISCOUNT,$sub_period);
			endPrice += (orgPrice*discount)/100;
		}
		return endPrice.toFixed(2);
		*/
	}
	
	function calPriceTotalUnDiscount(){
		$subscription_id = $('#subscription').val();
		$list_place = getArrayNameTextInput('list_place');
		$list_pack = getMultiCheckbox('list_pack');
		$list_priority = getMultiCheckbox('list_priority');
		
		$number_escort = $('#escorts_apply_number').val();
		var price = 0;
		price += returnValueJSON(subs,$subscription_id);
		
		for(var i=0; i<$list_place.length; i++){
			price += returnValueJSON(place,$list_place[i]);
		}
		for(var i=0; i<$list_pack.length; i++){
			price += returnValueJSON(pack,$list_pack[i]);
		}
		for(var i=0; i<$list_priority.length; i++){
			price += returnValueJSON(priority,$list_priority[i]);
		}
		$sub_period = $('#subscription_period').val();
		return ($sub_period*price*$number_escort).toFixed(2);
	}
	
	function cal_Subs(){
		$subscription_id = $('#subscription').val();
		$sub_period = $('#subscription_period').val();
		
		var price = returnValueJSON(subs,$subscription_id);
		var discount = returnValueJSON(DISCOUNT,$sub_period);
		var endPrice = price*$sub_period - (price*discount)/100;
		return endPrice;
	}
	
	function cal_Listing(){
		$list_place = getArrayNameTextInput('list_place');
		var price = 0;
		for(var i=0; i<$list_place.length; i++){
			var orgPrice = returnValueJSON(place,$list_place[i]);
			$sub_period = $('#subscription_period').val();
			
			var discount = returnValueJSON(DISCOUNT,$sub_period);
			var endPrice = orgPrice*$sub_period - (orgPrice*discount)/100;
			price += endPrice;
		}
		return price;
	}
	
	function cal_Pack(){
		$list_pack = getMultiCheckbox('list_pack');
		var price = 0;
		for(var i=0; i<$list_pack.length; i++){
			var orgPrice = returnValueJSON(pack,$list_pack[i]);
			$sub_period = $('#subscription_period').val();
			
			var discount = returnValueJSON(DISCOUNT,$sub_period);
			var endPrice = orgPrice*$sub_period - (orgPrice*discount)/100;
			price += endPrice;
		}
		return price;
	}
	
	function cal_Priority(){
		$list_priority = getMultiCheckbox('list_priority');
		var price = 0;
		for(var i=0; i<$list_priority.length; i++){
			var orgPrice = returnValueJSON(priority,$list_priority[i]);
			$sub_period = $('#subscription_period').val();
			
			var discount = returnValueJSON(DISCOUNT,$sub_period);
			var endPrice = orgPrice*$sub_period - (orgPrice*discount)/100;
			price += endPrice;
		}
		return price;
	}
	
	function cal_discount_priority(id,val){
		updatePriceJS();
	}
	
	function cal_discount_pack(id, val){
		updatePriceJS();
	}
	
	function cal_discount_listplace(id,val){
		updatePriceJS();
	}
	
	function cal_discount_subs(val){
		updatePriceJS();
	}
	
	function validateB4Submit(formData, jqForm, options){
		$escorts_apply_number = $('#escorts_apply_number').val();
		$('#escorts_apply_number').removeClass('red-border');
		if(!isNaturalNumber($escorts_apply_number) || $escorts_apply_number == 0){
			$('#escorts_apply_number').addClass('red-border');
			return false;
		}
		if(CONTRACT_ENABLE == 0){
			return false;
		}
        /**
		if($('#payment_gateway').val() != 'wirebank'){
			alert('<?php echo language_translate('services_page_payment_gateway_not_support');?>');
			return false;
		}
        **/
		$('#loader_image').show();
		CONTRACT_ENABLE = 0;
		return true;
	}

	function processAfterResponding(responseText, statusText, xhr, $form) {
		$('#loader_image').hide();	
		if($('#payment_gateway').val() == 'wirebank'){
			gotoPaymentPage();
		}else{
		  gotoCreditcardPaymentPage();
		}
		/*
		if(responseText == 'ok'){
			gotoPaymentPage();
		}else{
			debug(responseText);
		}
		*/
	}
    
    function gotoCreditcardPaymentPage(){
        queryurl(BASE_URI+"agent/transaction_submit/creditcard_method");
    }
	
	function gotoPaymentPage(){
		queryurl(BASE_URI+"agent/my_account/payment");
	}

	function deleteListingItem(id){
		$('#listing_id_'+id).remove();
		updatePriceJS();
	}
	
	function loadListingPlaceElement(listingId){
		$('#listingPlaceLoadingImage').toggle();

		country_id = $('#service_country_id').val();
		state_id = $('#service_state_id').val();
		province_id = $('#service_province_id').val();
		city_id = $('#service_city_id').val();
  
		$listingIdArray = getArrayNameTextInput('list_place');
		
		$.get(BASE_URI+'home/services_reg_async/fetch_listing_id',
				{country_id:country_id,state_id:state_id,province_id:province_id,city_id:city_id},
			function(listingId){
				if($listingIdArray.in_array(listingId)){
					$('#listingPlaceLoadingImage').toggle();
					return false;
				}else{
					$.get(BASE_URI+'agent/async/load_listing_place_element',{id:listingId},function(res){
						$('#listingPlaceLoadingImage').toggle();
						$('#listingPlaceTarget').append(res);
						$('#listing_id_'+listingId+' select.custom').customStyle();
						updatePriceJS();
					});
				}
			}	
		);
	}
	
</script>